package Saas_Exercises;

import java.util.Scanner;

public class Assignment05_Organism {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		String rowCol = null;
		if (sc.hasNextLine())
			rowCol = sc.nextLine();
		Scanner sc1 = new Scanner(rowCol);

		// populating the rows and columns variables
		int r = sc1.nextInt();
		int c = sc1.nextInt();

		System.out.println(r + " " + c); // Accepted row column values
		char[][] o_arr = new char[r][c]; // Creating a Mul-dim char array based
											// on the row and column values

		// populating the mul-dim array with characters(here the input didn't have any empty spaces)
		for (int i = 0; i < r; i++) {
			if (sc.hasNextLine()) {
				String str = sc.nextLine();

				for (int j = 0; j < o_arr[i].length; j++) {

					o_arr[i][j] = str.charAt(j);

				}

				// System.out.println(str);
				// o_arr[i] = str.toCharArray();
			} else
				System.out.println("Line not found!!!");
		}

		int no_of_iterations = sc.nextInt();
		System.out.println("no_of_iterations ::" + no_of_iterations);

		evaluate(o_arr, no_of_iterations);

	}

	private static void evaluate(char[][] o_arr, int no_of_iterations) {

		if (no_of_iterations > 0) {
			int no_of_living_organisms = 0;
			char newArr[][] = new char[o_arr.length][o_arr[0].length];

			for (int i = 0; i < o_arr.length; i++) {

				for (int j = 0; j < o_arr[i].length; j++) {

					int no_of_neighbors = getNumberOfNeighbors(o_arr, i, j);

					char c = o_arr[i][j];
					// System.out.println(no_of_neighbors);

					// check for organisms

					if (c == '@') {

						if (no_of_neighbors == 2 || no_of_neighbors == 3) {

							newArr[i][j] = '@';
							no_of_living_organisms++;

						} else if (no_of_neighbors >= 4) {

							newArr[i][j] = '#';

						} else {

							newArr[i][j] = '#';

						}

						// check for spaces
					} else if (c == '#') {

						if (no_of_neighbors == 3) {

							newArr[i][j] = '@';
							no_of_living_organisms++;
						}

						else {
							newArr[i][j] = '#';

						}

					}

				}

			}
			print(newArr);
			System.out.println("no_of_living_organisms"
					+ no_of_living_organisms);
			no_of_iterations--;
			evaluate(newArr, no_of_iterations);
		}

	}

	private static int getNumberOfNeighbors(char[][] o_arr, int i, int j) {

		int no_of_neighbors = 0;

		for (int k = 0; k < o_arr.length; k++) {

			for (int l = 0; l < o_arr[i].length; l++) {

				if (k == i - 1 && l == j - 1) {

					if (o_arr[k][l] == '@') {

						no_of_neighbors++;

					}

				}
				if (k == i - 1 && l == j) {
					if (o_arr[k][l] == '@') {

						no_of_neighbors++;
					}
				}
				if (k == i - 1 && l == j + 1) {
					if (o_arr[k][l] == '@') {

						no_of_neighbors++;
					}
				}
				if (k == i && l == j - 1) {
					if (o_arr[k][l] == '@') {

						no_of_neighbors++;
					}
				}
				if (k == i && l == j + 1) {
					if (o_arr[k][l] == '@') {

						no_of_neighbors++;
					}
				}
				if (k == i + 1 && l == j - 1) {
					if (o_arr[k][l] == '@') {

						no_of_neighbors++;
					}
				}
				if (k == i + 1 && l == j) {
					if (o_arr[k][l] == '@') {

						no_of_neighbors++;
					}
				}
				if (k == i + 1 && l == j + 1) {
					if (o_arr[k][l] == '@') {

						no_of_neighbors++;

					}

				}

			}

		}

		return no_of_neighbors;
	}

	private static void print(char[][] o_arr) {

		for (int i = 0; i < o_arr.length; i++) {

			System.out.println(new String(o_arr[i]));
		}

	}

}
